package com.msconfig.device;

import android.os.Handler;
import android.os.Looper;


import com.msconfig.event.DeviceUpdateUINotify;

import java.util.ArrayList;
import java.util.List;


// 命令发送管理 ;
public class CommandSendManage 
{
	// 单例模式获取实例
	private static CommandSendManage mCommandSendManage = null;
	private boolean isReturnData;//是否返回当前发送命令头

	public static CommandSendManage getInstance()
	{
		if (mCommandSendManage == null) 
		{
			mCommandSendManage = new CommandSendManage();
		}

		return mCommandSendManage;
	}

	// 处理RTK回复命令;
	public void processRtkBackCommand(String strGnssLine)
	{
		isReturnData=false;
		// 如果接收到发送命令的反馈消息;
		if (mSendCommandList != null && mSendCommandList.size() > 0)				
		{
			String strAnswerHead = mSendCommandList.get(0).strAnswerHead;
			//如果接收到当前命令的回复,则发下一送命令;
			if (strGnssLine.contains(strAnswerHead))
			{
				isReturnData=true;
				//通知界面,当前命令发送成功;
				DeviceUpdateUINotify.eventDeviceCommandSend(1, mSendCommandList.get(0).strFunction);
				// 获取并发送下一条命令;
				sendNextCommand();

				//下面判断是处理重传或者超时问题
			}else if(strGnssLine.startsWith("@GNSS,SET")){//针对设置操作，只要当前设置的命令没有返回，就显示超时，
				mHandler.post(mRunnable);
			}else{//针对像GETALL等命令，等待3s后在进行判断，isReturnData还是为false代表没有数据回复，重新发送
				//防止GETALL多次发送
				mHandler.postDelayed(mRunnable,3000);
			}
		}
	}

	//设置命令发送列表;
	public void setSendCommandList(List<TagSendCommand> SendCommand) {
		synchronized (object){
			mSendCommandList = SendCommand;
		}
	}

	public int getCommandSize() 
	{
		if (mSendCommandList != null) 
		{
			return mSendCommandList.size();
		}
		return 0;
	}

	// 功能：开始发送指令; 
	public boolean startSendCommand()
	{
		if (getCommandSize() <= 0)
		{
			stopSendCommand();
			return false;
		}

		// 开始发送第一条命令;
		sendCommand(mSendCommandList.get(0));

		return true;
	}
	// ========================================================
	// 功能：停止发送命令
	// 输入：无
	// 输出：无
	// 返回：无
	// 日期：2006.02.17
	// ========================================================
	public boolean stopSendCommand()
	{
		if (mSendCommandList != null)
		{
			mSendCommandList.clear();
		}

		//在结束的时候,关闭计时器;
		mHandler.removeCallbacks(mRunnable);
		return true;
	}	

	// =====================================================
	// 功能：获取下一个发送命令;
	// 输入：无
	// 输出：无
	// 返回：无
	// 日期：2006.02.17
	// =====================================================
	private boolean sendNextCommand()
	{
		if (getCommandSize() <= 0)
		{
			return false;
		}
		//删除第一条命令;
		mSendCommandList.remove(0);
		
		if (getCommandSize() <= 0)
		{
			stopSendCommand();

			DeviceUpdateUINotify.eventDeviceCommandSend(3,"");
			return false;
		}
		
		// 发送下一条命令;
		sendCommand(mSendCommandList.get(0));
		return true;
	};
	
	// ============================================================
	// 功能：发送命令;
	// 输入：无;
	// 输出：无;
	// 返回：无;
	// 日期：2006.02.17;
	// ============================================================
	private boolean sendCommand(TagSendCommand command)
	{
		if (command == null/* || (command.strCommand.isEmpty()&&command.pHexCmd==null)*/)
		{
			return false;
		}

		if (command.strCommand.length() > 0)
		{
			String strSend = command.strCommand + "\r\n";
			DeviceManage.getInstance().sendCommand(strSend);
		}

		// 通知界面,开始发送命令;
		DeviceUpdateUINotify.eventDeviceCommandSend(0, command==null?"":command.strFunction);
		return true;
	}
	
	// 等待发送的一起信息数组;
	private List<TagSendCommand> mSendCommandList = new ArrayList<TagSendCommand>();

	// 线程处理命令重发问题 ;
	private final Object object=new Object();
	private Handler mHandler = new Handler(Looper.getMainLooper());
	private Runnable mRunnable = new Runnable()
	{
		@Override
		public void run()
		{
			//当前无可发送命令
			if (getCommandSize() <= 0)
			{
				return;
			}

			try {
				//正在发送命令;
				TagSendCommand temCommand = mSendCommandList.get(0);
				// 还是没有数据的话直接超时处理
				if (temCommand.nTimeOut < 0)
				{
					if (!temCommand.strFunction.isEmpty())
					{
						// 提示命令超时 (超时处理) ;
						DeviceUpdateUINotify.eventDeviceCommandSend(2, temCommand.strFunction);
					}
					//超时的话就关闭一切退出
					stopSendCommand();
					return;

				} else {
					//回复头不是为空的时候,按照原来的套路执行;
					temCommand.nTimeOut--;

					// 修改为按照一定间隔发送;
					int nInterval = 3;
					nInterval = temCommand.nInterval;
					if (nInterval <= 0 || nInterval > 1000) {
						nInterval = 3;
					}
					//处理重发 9,6,3,0
					if (temCommand.nTimeOut % nInterval == 0)   //每隔3秒发送一次命令;
					{
						if(!isReturnData){//当前发送命令没有命令头返回
							sendCommand(temCommand);
						}else{//成功返回数据
							mHandler.removeCallbacks(mRunnable);
						}
					}

				}
			}catch (Exception e){}

			//1秒进行一次;
			mHandler.postDelayed(this, 1000);
		}
	};

}
